/*
Hey you! You are reading my code!
I want to say that my code is far from perfect, and if you see that I'm doing something
in a really wrong/dumb way, please give me advices instead of saying "LOL U BAD CODER"
        Thanks
      - MadJawa
*/
AddCSLuaFile( "cl_init.lua" );
AddCSLuaFile( "shared.lua" );

include( "shared.lua" );

resource.AddFile( "materials/VGUI/entities/gmod_laser_killicon.vtf" );
resource.AddFile( "materials/VGUI/entities/gmod_laser_killicon.vmt" );


function ENT:Initialize()

	self.Entity:PhysicsInit( SOLID_VPHYSICS );
	self.Entity:SetMoveType( MOVETYPE_VPHYSICS );
	self.Entity:SetSolid( SOLID_VPHYSICS );
	
	local phys = self.Entity:GetPhysicsObject();
	if ( phys:IsValid() ) then
		phys:Wake();
	end
	
	self.Targets = {};
	
	if WireAddon then
		self.Inputs = Wire_CreateInputs( self.Entity, { "On" } );
		self.Outputs = Wire_CreateOutputs( self.Entity, { "On", "Length", "Width", "Damage", "Hit", "Rate" } );
	end
        RD_Register(self, true)--For Environments Compatibility

end


function ENT:Think()
	if ( self:GetOn() ) then
		local power = self:GetResourceAmount("energy")
		if(power>=self:GetRateAmmount())then
			self:ConsumeResource("energy",self:GetRateAmmount())
		else
			self:SetOn(false) return
		end	
		local target = LaserLib.DoBeam( self.Entity, self:GetBeamDirection(), self:GetBeamStart(), self:GetBeamLength(), self:GetDamageAmmount());
		if target.Link and target.LaserConvert then 
			target:SupplyResource("energy",self:GetRateAmmount()*0.8)
		else
			target.EnergyRates = self:GetRateAmmount()*0.6
		end
		if WireAddon then
			if ( target and target:IsValid() ) then
				Wire_TriggerOutput( self.Entity, "Hit", 1 );
			else
				Wire_TriggerOutput( self.Entity, "Hit", 0 );
			end
		end
		Wire_TriggerOutput( self.Entity, "Rate", self:GetRateAmmount() );
		self.Entity:NextThink( CurTime() + 1 );
		return true;
	end
end


function ENT:TriggerInput( iname, value )

	if ( iname == "On" ) then
		local power = self:GetResourceAmount("energy")
		if(power>=self:GetRateAmmount())then
			self:ConsumeResource("energy",self:GetRateAmmount())
			self:SetOn( util.tobool( value ) );
		else
			self:SetOn(false)
		end
	end

end



local function On( ply, ent )

	if ( not ent or ent == NULL ) then return; end
	ent:SetOn( !ent:GetOn() );

end

local function Off( ply, ent )

	if ( not ent or ent == NULL or ent:GetToggle() ) then return; end
	ent:SetOn( !ent:GetOn() );

end

numpad.Register( "Laser_On", On ) 
numpad.Register( "Laser_Off", Off ) 